﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace TinyCinema.Data
{
    public class MenuOrder:Execution
    {
        public string ID { get; set; }
        public DateTime OrderTime { get; set; }
        public decimal Value { get; set; }
        public Employee Employee { get; set; }
        public ObservableCollection<KeyValuePair<Menu, int>> OrderDetail { get; set; }
        public Customer Customer { get; set; }
        public MenuOrder(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.MenuOrder);
            using (var tbl = ds.Tables[Tables.MenuOrder])
            {
                var Row = tbl.Select(MenuOrderTable.ID + "='" + ID + "'")[0];
                OrderTime = (DateTime)Row[MenuOrderTable.OrderTime];
                Value = (decimal)Row[MenuOrderTable.Value];
                Employee = new Employee(Row[MenuOrderTable.EmployeeID] as string);
                Customer = new Customer(Row[MenuOrderTable.CustomerID] as string);
            }
            PutTableIntoDataSet(Tables.MenuOrderDetail);
            using(var tbl = ds.Tables[Tables.MenuOrderDetail])
            {
                OrderDetail = new ObservableCollection<KeyValuePair<Menu, int>>();
                var Rows = tbl.Select(MenuOrderDetailTable.MenuOrderID + "='" + ID + "'");
                foreach(var Row in Rows)
                    OrderDetail.Add(new KeyValuePair<Menu, int>(new Menu(Row[MenuOrderDetailTable.MenuID] as string), (int)Row[MenuOrderDetailTable.Quantum]));                
            }
        }
        public MenuOrder(Customer customer,Employee employee,ObservableCollection<KeyValuePair<Menu, int>> detail)
        {
            ID = IDGenerate(Tables.MenuOrder);
            Customer = customer;
            Employee = employee;
            OrderDetail = detail;
            OrderTime = DateTime.Now;
            Value = 0;
            foreach(var d in detail)
            {
                Value += d.Key.Price * d.Value;
            }
            Save();
        }
        public void Save()
        {
            PutTableIntoDataSet(Tables.MenuOrder);
            using(var tbl = ds.Tables[Tables.MenuOrder])
            {
                var Row = tbl.NewRow();
                Row[MenuOrderTable.ID] = ID;
                Row[MenuOrderTable.OrderTime] = OrderTime;
                if (Customer != null) Row[MenuOrderTable.CustomerID] = Customer.ID;
                else Row[MenuOrderTable.CustomerID] = "KH000000";
                if (Employee != null) Row[MenuOrderTable.EmployeeID] = Employee.ID;
                Row[MenuOrderTable.Value] = Value;
                tbl.Rows.Add(Row);
                SaveCommand(Tables.MenuOrder);
            }

            PutTableIntoDataSet(Tables.MenuOrderDetail);
            using(var tbl=ds.Tables[Tables.MenuOrderDetail])
            {
               foreach(var Order in OrderDetail)
               {
                   var Row = tbl.NewRow();
                   Row[MenuOrderDetailTable.MenuID] = Order.Key.ID;
                   Row[MenuOrderDetailTable.MenuOrderID] = ID;
                   Row[MenuOrderDetailTable.Quantum] = Order.Value;
                   tbl.Rows.Add(Row);
               }
               SaveCommand(Tables.MenuOrderDetail);
            }
            
        }
        public static ObservableCollection<MenuOrder> Filter(DateTime from, DateTime to, string CustomerID)
        {
            PutTableIntoDataSet(Tables.MenuOrder);
            var tbl = ds.Tables[Tables.MenuOrder];
            var Rows = tbl.Select(MenuOrderTable.OrderTime + " >= '" + from + "' and " + MenuOrderTable.OrderTime + " <= '" + to + "' and " + MenuOrderTable.CustomerID + "='" + CustomerID + "'");
            var Result = new ObservableCollection<MenuOrder>();
            foreach (var Row in Rows)
            {
                Result.Add(new MenuOrder(Row[MenuOrderTable.ID] as String));
            }
            return Result;
        }
    }
}
